package tool.library.widget;

import android.content.Context;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.AttributeSet;

import tool.library.adapter.BaseRecyclerAdapter;

/**
 * Created by xzp on 15/9/23.
 */
public abstract class LoadRecyclerView extends RecyclerView{
    protected static final byte LOAD_INIT=1;
    protected static final byte LOAD_START=2;
    protected static final byte LOAD_FINISH=3;
    protected static final byte LOAD_COMPLETE=4;
    private byte mState;
    private ViewHolder mLoadHolder;
    private OnLoadListener mLoadListener;
    public LoadRecyclerView(Context context) {
        super(context);
        init(context);
    }

    public LoadRecyclerView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init(context);
    }

    public LoadRecyclerView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        init(context);
    }
    private void init(Context context)
    {
        mLoadHolder=initLoadHolder(context);
        this.addOnScrollListener(mScrollListener);
        changeState(LOAD_INIT);
    }

    private OnScrollListener mScrollListener=new OnScrollListener() {
        @Override
        public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
            super.onScrolled(recyclerView, dx, dy);
            if(mState==LOAD_FINISH && dy>0)
            {
                int itemCount=getAdapter().getItemCount();
                int lastChildPosition= ((LinearLayoutManager) getLayoutManager()).findLastVisibleItemPosition();
                if(lastChildPosition>=itemCount-1){
                    if(mLoadListener!=null)
                    {
                        mLoadListener.onLoad(LoadRecyclerView.this);
                        changeState(LOAD_START);
                    }
                }
            }
        }
    };
    public final boolean isLoad()
    {
        return mState==LOAD_START;
    }
    protected final OnLoadListener getLoadListener() {
        return mLoadListener;
    }

    public void setOnLoadListener(OnLoadListener loadListener) {
        this.mLoadListener = loadListener;
    }
    public void loadFinish()
    {
        changeState(LOAD_FINISH);
    }

    public void loadComplete()
    {
        changeState(LOAD_COMPLETE);
    }

    protected final void changeState(byte state)
    {
        if(mState==state)
            return;
        mState=state;
        switch (state)
        {
            case LOAD_INIT:
                onInit();
                break;
            case LOAD_START:
                onStart();
                break;
            case LOAD_FINISH:
                onFinish();
                break;
            case LOAD_COMPLETE:
                onComplete();
                break;
        }
    }
    protected abstract ViewHolder initLoadHolder(Context context);
    protected abstract void onInit();
    protected abstract void onStart();
    protected abstract void onFinish();
    protected abstract void onComplete();

    @Override
    public void setAdapter(Adapter adapter) {
        if(adapter!=null && adapter instanceof BaseRecyclerAdapter){
            BaseRecyclerAdapter recyclerAdapter= (BaseRecyclerAdapter) adapter;
            recyclerAdapter.setLoadHolder(mLoadHolder);
        }
        super.setAdapter(adapter);
    }
    public interface OnLoadListener{
        void onLoad(LoadRecyclerView recyclerView);
    }
}
